
#import "LLCourseTitleTableViewCell.h"
#define SCREEN  [UIScreen mainScreen].bounds.size

@implementation LLCourseTitleTableViewCell {
    UILabel * titleLabel;
    UILabel * placeholderLabel;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.textView = [[UITextView alloc]initWithFrame:CGRectMake(24, 17, LLScreenWidth - 48, 34)];
        self.textView.delegate = self;
        self.textView.font = LLRegularFont(15);
        self.textView.scrollEnabled = NO;
        self.textView.showsVerticalScrollIndicator = NO;
        self.textView.showsHorizontalScrollIndicator = NO;
        self.textView.returnKeyType = UIReturnKeyDone;
        self.textView.keyboardType = UIKeyboardTypeDefault;
        
        UIView *dismissView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN.width, 40)];
        dismissView.backgroundColor = [UIColor whiteColor];
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN.width-50, 0, 40, 40)];
        [button setTitle:@"完成" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(dismissButtonAction) forControlEvents:UIControlEventTouchUpInside];
        [dismissView addSubview:button];
        _textView.inputAccessoryView = dismissView;
        [self.contentView addSubview:_textView];
        
        placeholderLabel=[[UILabel alloc] initWithFrame:CGRectMake(2, -8, LLScreenWidth-34, 44)];
        placeholderLabel.text=@"请输入标题";
        placeholderLabel.textColor=[UIColor lightGrayColor];
        placeholderLabel.numberOfLines = 0;
        placeholderLabel.font=LLRegularFont(15);
        [_textView addSubview:placeholderLabel];
    }
    return self;
}

- (void)dismissButtonAction {
    [_textView resignFirstResponder];
    [self.tableView reloadData];
}

- (void)setContentStr:(NSString *)contentStr {
    _contentStr = contentStr;
    _textView.text = _contentStr;
    if (_textView.text.length == 0) {
        placeholderLabel.hidden = NO;
    } else {
        placeholderLabel.hidden = YES;
    }
    CGRect frame = _textView.frame;
    CGSize constraintSize = CGSizeMake(frame.size.width, MAXFLOAT);
    CGSize size = [_textView sizeThatFits:constraintSize];
    _textView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, size.height);
}

- (void)setIndexPath:(NSIndexPath *)indexPath {
    _indexPath = indexPath;
}

- (void)setPlaceholder:(NSString *)placeholder {
    _placeholder = placeholder;
    placeholderLabel.text = _placeholder;
    CGFloat hei = [CommonTools sizeOfText:_placeholder fontSize:15 width:LLScreenWidth -48-4 height:MAXFLOAT].height;
    _textView.frame = CGRectMake(24, 17, LLScreenWidth - 48, hei+20);
    placeholderLabel.frame = CGRectMake(2, 4, LLScreenWidth - 48-4, hei);
}

- (CGFloat)CellHeight {
    CGSize size = [_textView sizeThatFits:CGSizeMake(_textView.frame.size.width, MAXFLOAT)];
    return size.height+17;
}

-(void)textViewDidChange:(UITextView *)textView{
    if (textView.text.length==0) {
        placeholderLabel.hidden = NO;
    } else {
        placeholderLabel.hidden = YES;
    }
    if ([self.delegate respondsToSelector:@selector(updatedText:atIndexPath:)]) {
        [self.delegate updatedText:textView.text atIndexPath:_indexPath];
    }
    CGRect frame = textView.frame;
    CGSize constraintSize = CGSizeMake(frame.size.width, MAXFLOAT);
    CGSize size = [textView sizeThatFits:constraintSize];
    textView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, size.height);
    [_tableView beginUpdates];
    [_tableView endUpdates];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        [self.tableView reloadData];
    }
    return YES;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
